//
//  MainViewController.swift
//  Weibo
//
//  Created by zxy on 2018/11/28.
//  Copyright © 2018 zxy. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    //懒加载
    private lazy var AddButton: UIButton = {
        let w = UIScreen.main.bounds.size.width / CGFloat(viewControllers!.count)
        let h = tabBar.frame.size.height
        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: w*2, y: 0, width: w, height: h)
        
        //设置前景图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        btn.addTarget(self, action: #selector(AddBtnClick(addBtn:)), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupChildController()
    }
    
    //创建tabBar不能放到viewDidLoad，因为此时tabBar还没加载
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(AddButton)
    }
    
    private func setupChildController(){
        //加载本地json文件动态创建控制器,
        let path = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil)

        if let jsonPath = path{
        let jsondata = NSData(contentsOfFile: jsonPath);
        //捕获异常
        do{//可能发生异常的代码
            let vcListArray = try JSONSerialization.jsonObject(with: jsondata! as Data, options: .mutableContainers)
            //遍历数组 swift:遍历数组需要设置数组类型
            for dict in vcListArray as! [[String: String]]{
                setupTabBarController(MainControllerName: dict["vcName"]!, title: dict["title"]!, normalImageName: dict["imageName"]!)
        }
        }catch{//异常发生,手动加载
                print(error)
                //首页
                setupTabBarController(MainControllerName: "HomeTableViewController", title: "首页", normalImageName: "tabbar_home")

                //消息
                setupTabBarController(MainControllerName:  "MessageTableViewController", title: "消息", normalImageName: "tabbar_message_center")


                setupTabBarController(MainControllerName: "AddViewController", title: "", normalImageName: "")

            

                //发现
                setupTabBarController(MainControllerName:  "DiscoverTableViewController", title: "发现", normalImageName: "tabbar_discover")

                //我
                setupTabBarController(MainControllerName:  "ProfileTableViewController", title: "我", normalImageName: "tabbar_profile")
            }
        }
    }
    
    //MARK:-创建tabBarController  private:私有方法，只能在本类调用
    private func setupTabBarController(MainControllerName: String,title: String,normalImageName:String){
//        let nav = UINavigationController(rootViewController: MainController)
//        nav.tabBarItem = UITabBarItem(title: title, image: UIImage(named: normalImageName)?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: normalImageName + "_highlighted")?.withRenderingMode(.alwaysOriginal))
//        addChild(nav)
        //动态获取命名空间 swift中控制器：命名空间.控制器名称
        let ns = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        //字符串转类,并转成UIViewController类型
        let cls = NSClassFromString(ns + "." + MainControllerName) as! UIViewController.Type
        
        //创建类
        let ctl = cls.init()
        let nav = UINavigationController(rootViewController: ctl)
        nav.tabBarItem = UITabBarItem(title: title, image: UIImage(named: normalImageName)?.withRenderingMode(.alwaysOriginal), selectedImage: UIImage(named: normalImageName + "_highlighted")?.withRenderingMode(.alwaysOriginal))
        nav.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:UIColor.orange], for: .selected)
        addChild(nav)
    }

    //MARK:-中间+号按钮点击事件
    @objc func AddBtnClick(addBtn: UIButton){
        print(#function)
    }
    
}
